﻿namespace PowerSong.UnitTests
    {
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using PowerSong.SongDatabase;

    [TestClass]
    public class SongTests
        {

        [TestMethod]
        public void CreateSongTest()
            {

            // Create a database with one song in it
            var database = Database.Create();
            var songToInsert = CreateSong();
            database.Songs.Write(songToInsert);
            var retrievedSong = database.Songs.Get(songToInsert.ID);

            // Confirm the song was properly created
            AssertSong(songToInsert, "Title", new string[] { "Verse 1", "Verse 2", "Verse 3" }, new string[] { "Author 1", "Author 2" });

            }

        [TestMethod]
        public void ChangeTitleTest()
            {

            // Create a database with one song in it
            var database = Database.Create();
            var songToInsert = CreateSong();
            database.Songs.Write(songToInsert);

            // Change the song title
            var songToAlter = database.Songs.Get(songToInsert.ID);
            Assert.AreEqual("Title", songToAlter.Title);
            songToAlter.Title = "New Title";
            database.Songs.Write(songToAlter);

            // Confirm the song title was properly changed
            var retrievedSong = database.Songs.Get(songToInsert.ID);
            Assert.AreEqual("New Title", retrievedSong.Title);

            }

        private void AssertSong(Song song, string title, string[] verses, string[] authors)
            {
            Assert.IsNotNull(song);
            Assert.AreEqual(title, song.Title);
            Assert.IsTrue(song.Verses.SequenceEqual(verses));
            Assert.IsTrue(song.Authors.SequenceEqual(authors));
            }

        private Song CreateSong(string title = "Title")
            {
            var song = new Song(title, "Verse 1", "Verse 2", "Verse 3");
            song.Authors.Add("Author 1");
            song.Authors.Add("Author 2");
            song.Copyright = "Copyright line";
            return song;
            }

        }

    }
